'use strict';

/**
 * @ngdoc directive
 * @name jgappApp.directive:FileUpload
 * @description
 * # FileUpload
 */
angular.module('jgappApp')
    .directive('fileUpload', ['FileService', 'Util', 'openApi', "OpenFile", function (FileService, Util, openApi, OpenFile) {
        return {
            scope: {
                fileData: "=fileData"
            },
            template: '<div class="c9">附件</div>' +
            '<ul class="file-list cl">' +
            '<img src="images/icon_addpic.png" ng-click="addpic()" ng-if="!showdel"/>' +
            // '<div class="c-succ" ng-repeat="item in fileArr track by $index"><span ng-click="goDetail(item)">  {{item|fileName}}</span><span class="c-red fr" ng-click="delpic($index)" ng-if="!showdel">删除附件</span></div>' +
            '<li ng-repeat="item in fileArr track by $index" ng-class="{img:isImg(item)}"><img ng-click="goDetail(item)" ng-src="{{item|showimg}}" ng-if="item|ifimg" />' +
            '<span class="c-succ" ng-click="goDetail(item)" ng-if="item|iffile">{{item|fileName}}</span>' +
            '<span class="c-red fr" ng-click="delpic($index)" ng-if="!showdel" ng-class="{icon_del1:isImg(item)}">删除附件</span>' +
            '</li>' +
            '</ul>',
            restrict: 'AE',
            link: function postLink(scope, element, attrs) {
                scope.showdel = attrs.checked; //审批页面则不显示
                var maxnum = attrs.maxnum || 7; //审批页面则不显示
                scope.fileArr = []; //审批页面则不显示
                scope.$watch('fileData', function (newValue, oldValue) {
                    if (newValue) {
                        scope.fileArr = newValue.split("|"); //图片数组
                    }
                });
                var cameraSuccess = function (imageData) {
                    FileService.upload(imageData, openApi.uploadFile).then(function (data) {
                        if (scope.fileData) {
                            scope.fileData = scope.fileData + "|" + data
                        } else {
                            scope.fileData = data
                        }
                    });
                };
                var cameraError = function () {

                };
                scope.cancel = function () {
                    Util.hideFile()
                };
                scope.choosePic = function (btn) {
                    Util.hideFile();
                    var cameraOptions = {
                        quality: 30,
                        EncodingType: 0,
                        destinationType: 1
                    };
                    cameraOptions.sourceType = btn
                    if (btn == 0) {
                        return
                    }
                    navigator.camera.getPicture(cameraSuccess, cameraError, cameraOptions);
                }
                var delPic = function (item, index) {
                    return function (btn) {
                        if (btn == 1) {
                            FileService.delfile(item).then(function (data) {
                                Util.Toast(data)
                                scope.fileArr.splice(index, 1)
                                scope.fileData = scope.fileArr.join("|")
                                scope.src = ""
                            });
                        }
                    }
                }
                scope.isFile = Util.isFile
                scope.isImg = Util.isImg
                scope.goDetail = function (fileData) {
                    if (Util.isImg(fileData)) {
                        scope.src = openApi.showimg + "?fileName=" + fileData;
                        Util.showFile(scope);
                    } else if (Util.isFile(fileData)) {
                        OpenFile.open(fileData)
                    } else {
                        Util.Toast("移动端不支持预览")
                    }
                };
                scope.closefile = function () {
                    Util.hideFile();
                }
                scope.addpic = function () {
                    if (scope.fileArr.length == maxnum) {
                        Util.Toast("只能上传" + maxnum + "个附件")
                        scope.maxlength = true
                        return
                    }
                    Util.chooseFile({
                        scope: scope
                    })
                    // navigator.notification.confirm(
                    //     '附件选择方式',
                    //     choosePic,
                    //     '添加附件', ['拍照', '相册']
                    // )
                }
                scope.delpic = function (item, index) {
                    navigator.notification.confirm(
                        '确定删除附件',
                        delPic(item, index),
                        '提示', ['确定', '取消']
                    )
                }

            }
        };
    }]);
